package io.mya.bian.time;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.binance.api.client.domain.TimeInForce;
import com.binance.api.client.domain.account.NewOrderResponse;

import io.mya.bian.entity.CurrencyEntity;
import io.mya.bian.service.BianService;
import com.binance.api.client.BinanceApiClientFactory;
import com.binance.api.client.BinanceApiRestClient;
import com.binance.api.client.domain.TimeInForce;
import com.binance.api.client.domain.account.NewOrderResponse;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.account.request.AllOrdersRequest;
import com.binance.api.client.domain.account.request.CancelOrderRequest;
import com.binance.api.client.domain.account.request.OrderRequest;
import com.binance.api.client.domain.market.TickerPrice;
import com.binance.api.client.exception.BinanceApiException;
import static com.binance.api.client.domain.account.NewOrder.limitBuy;
import static com.binance.api.client.domain.account.NewOrder.limitSell;

@Component
@EnableScheduling
public class TradingTime {
	@Autowired
	private BianService bianService;
	
	@Scheduled(fixedRate=1000)
	@Async
	public void buy(){
		String proxyHost = "127.0.0.1";
	    String proxyPort = "1080";
	    System.setProperty("http.proxyHost", proxyHost);
	    System.setProperty("http.proxyPort", proxyPort);
	    System.setProperty("https.proxyHost", proxyHost);
	    System.setProperty("https.proxyPort", proxyPort);
	    
		
		List<CurrencyEntity> list = bianService.queryList();
		for(CurrencyEntity currencyEntity:list){
			if (currencyEntity.getType() == 1 && currencyEntity.getStatus() == 1) {
				BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(currencyEntity.getApiKey(), currencyEntity.getSecret());
				BinanceApiRestClient client = factory.newRestClient();
				NewOrderResponse limitBuy = client.newOrder(limitBuy(currencyEntity.getCoinName(), TimeInForce.GTC, currencyEntity.getCount().toString(), currencyEntity.getUnitPrice().toString()));
			}
		}
	}
	@Scheduled(fixedRate=1000)
	@Async
	public void sell(){
		String proxyHost = "127.0.0.1";
	    String proxyPort = "1080";
	    System.setProperty("http.proxyHost", proxyHost);
	    System.setProperty("http.proxyPort", proxyPort);
	    System.setProperty("https.proxyHost", proxyHost);
	    System.setProperty("https.proxyPort", proxyPort);
		List<CurrencyEntity> list = bianService.queryList();
		for(CurrencyEntity currencyEntity:list){
			if (currencyEntity.getType() == 1 && currencyEntity.getStatus() == 2) {
				BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance(currencyEntity.getApiKey(), currencyEntity.getSecret());
				BinanceApiRestClient client = factory.newRestClient();
				NewOrderResponse limitSell = client.newOrder(limitSell(currencyEntity.getCoinName(), TimeInForce.GTC, currencyEntity.getCount().toString(), currencyEntity.getUnitPrice().toString()));
			}
		}
	}
}
